/**
 * 
 */
package com.lmm.test;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie2;
import org.apache.http.util.EntityUtils;

/**
 * @author leiming
 *
 */
public class ClientWithResponseHandler {
	public final static void main(String[] args) throws Exception {
		 BasicCookieStore cookieStore = new BasicCookieStore();
		 BasicClientCookie2 cookie1 = new BasicClientCookie2("JSESSIONID","C97DB3EE7DB4340B7173DE892C70FBDC");
		 cookie1.setDomain("127.0.0.1");
		 cookie1.setComment("test_cookie");
		 cookie1.setPath("/");
		 cookie1.setExpiryDate(new Date(2014,12,12));
		 cookieStore.addCookie(cookie1);
		HttpClientBuilder httpClientBuilder =HttpClientBuilder.create();
        //CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpClient  httpclient = httpClientBuilder.setDefaultCookieStore(cookieStore).build();
	
        try {
            HttpGet httpget = new HttpGet("http://127.0.0.1:8080/steel/diagnosis/interface/diagnosisInterfacesUrl.pfv?urlId=1&userName=湖北中拓建材");
        	//HttpGet httpget = new HttpGet("http://127.0.0.1:8080/test3/test.jsp");
        	Header[] headers = httpget.getAllHeaders();
        	for(Header h:headers){
        		System.out.println("r_name:"+h.getName()+" r_value:"+h.getValue());
        	}
            System.out.println("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                public String handleResponse(
                        final HttpResponse response) throws ClientProtocolException, IOException {
                	Header[] headers = response.getAllHeaders();
                	for(Header h:headers){
                		System.out.println("name:"+h.getName()+" value:"+h.getValue());
                	}
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
            List<Cookie> cookies = cookieStore.getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }
        } finally {
            httpclient.close();
        }
    }
}
